<template>
  <div class="app-container">
    <el-button round style="font-size: 12px" @click="back"><i class="el-icon-back"></i>返回</el-button>
    <br><br>
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="房间名称">
        <el-input
          v-model="queryParams.roomName"
          placeholder="请输入房间名称"
          clearable
          size="small"
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="getList">搜索</el-button>
        <el-button type="primary" size="mini" @click="onAdd">新增</el-button>
      </el-form-item>
    </el-form>

    <span class="spanClass">
      <el-card class="box-card" v-for="l in rooms">
      <div slot="header" class="clearfix">
        <span><el-link>{{ l.roomName }}</el-link></span>
        <i class="el-icon-tickets" style="margin-left: 190px"></i>
      </div>
      房间编号：{{l.roomId}} <br><br>
      房间面积：{{l.roomArea}}<br><br>
        <!--      <div>楼栋总房间数：{{l.loudongNum}}</div>-->
      <el-button icon="el-icon-edit" size="mini" style="float: left" @click="onUpdate(l)"></el-button>
      <el-button icon="el-icon-delete" size="mini" style="float: right" @click="del(l.id)"></el-button>
    </el-card>
    </span>

    <el-dialog
      title="新增/修改房间"
      :visible.sync="dialogVisible">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm">
        <el-form-item label="房间名称" prop="roomName" label-width="100px">
          <el-input v-model="ruleForm.roomName"></el-input>
        </el-form-item>
        <el-form-item label="房间编号" prop="roomId" label-width="100px">
          <el-input v-model="ruleForm.roomId"></el-input>
        </el-form-item>
        <el-form-item label="房间面积" label-width="100px">
          <el-input v-model="ruleForm.roomArea"></el-input>
        </el-form-item>
        <el-form-item label-width="100px">
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="dialogVisible=false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {del, getList, saveOrUpdate} from "@/api/site/room";

export default {
  name: "room",
  data(){
    return{
      dialogVisible:false,
      rules: {
        roomName: [
          {required: true, message: '请输入房间名称', trigger: 'blur'},
        ],
        roomId: [
          {required: true, message: '请输入房间编号', trigger: 'blur'},
        ],
      },
      ruleForm:{},
      queryParams:{
        roomName:'',
        floorId:''
      },
      rooms:''
    }
  },
  created() {
    this.queryParams.floorId=this.$route.query.floorId
    this.getList()
  },
  methods:{
    onUpdate(row){
      this.ruleForm=row
      this.dialogVisible=true
    },
    onAdd(){
      this.dialogVisible=true
      this.ruleForm={
        roomName:'',
        roomId:"",
        roomArea:null,
        floorId:null
      }
    },
    submitForm(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.ruleForm.floorId=this.queryParams.floorId
          saveOrUpdate(this.ruleForm).then(res=>{
            this.$message.success('操作成功')
            this.dialogVisible=false
            this.getList()
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    back(){
      this.$router.back()
    },
    getList(){
      getList(this.queryParams).then(res=>{
        this.rooms=res.data
      })
    },
    del(id){
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(id).then(res=>{
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
  }
}
</script>

<style scoped>
.spanClass div{
  display: inline-block;
}
.box-card{
  width: 300px;
  font-size: 14px;
  padding: 6px;
  margin: 5px;
}
</style>
